package com.ggh.jinjilive.ui.mall.shopping.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemShopPingCarBinding;
import com.ggh.jinjilive.databinding.ItemShopPingCarGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;
import com.ggh.jinjilive.utils.GeneralUtils;

import java.util.List;

public class ShopingCarGoodsAdapter extends RecyclerView.Adapter<ShopingCarGoodsAdapter.ItemViewHolder> {


    private List<GoodsShopSPMobile> goodsShopSPMobileList;

    public void setAdd(List<GoodsShopSPMobile> goodsShopSPMobileList) {
        this.goodsShopSPMobileList = goodsShopSPMobileList;
        notifyDataSetChanged();
    }


    public void Add(List<GoodsShopSPMobile> goodsShopSPMobileList) {
        if(null != this.goodsShopSPMobileList){
            this.goodsShopSPMobileList.addAll(goodsShopSPMobileList);
        }else {
            this.goodsShopSPMobileList = goodsShopSPMobileList;
        }
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }



    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemShopPingCarGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_shop_ping_car_goods, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ItemViewHolder holder,final int position) {
        GoodsShopSPMobile goodsShopSPMobile = goodsShopSPMobileList.get(position);
        if(null != goodsShopSPMobile){
            //商品图片
            Glide.with(holder.binding.storeImg).load(goodsShopSPMobile.getProduct_img()).into(holder.binding.storeImg);
            //商品名称
            holder.binding.tvGoodsProductName.setText(goodsShopSPMobile.getProduct_name());
            //规格
            holder.binding.tvSpecification.setText(goodsShopSPMobile.getProduct_spec_name());
            //价格
            holder.binding.tvPrice.setText(goodsShopSPMobile.getProduct_spec_fee() +"");
            //数量
            holder.binding.etMagnitude.setText(goodsShopSPMobile.getNum() +"");
            if(goodsShopSPMobile.isSelected()){//是否选中
                holder.binding.cbCheckall.setChecked(true);
            }else {
                holder.binding.cbCheckall.setChecked(false);
            }
            //CheckBox设置事件监听
            holder.binding.cbCheckall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    goodsShopSPMobile.setSelected(isChecked);
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(99997,goodsShopSPMobileList);
                    }
                }
            });
            //减
            holder.binding.imgSubtract.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(goodsShopSPMobileList.get(position).getNum() == 1){
                        ToastUtils.make().show("数量低于范围~");
                        return;
                    }
                    goodsShopSPMobileList.get(position).setNum(goodsShopSPMobileList.get(position).getNum() - 1);
                    //更新购物车数量
                    GeneralUtils.editCartNumber(goodsShopSPMobile.getId(),goodsShopSPMobileList.get(position).getNum());
                    //数量
                    holder.binding.etMagnitude.setText(goodsShopSPMobile.getNum() +"");
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(99998,goodsShopSPMobileList);
                    }
                }
            });
            //加
            holder.binding.imgAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(goodsShopSPMobile.getNum() < goodsShopSPMobile.getProduct_spec_stock()){
                        goodsShopSPMobileList.get(position).setNum(goodsShopSPMobileList.get(position).getNum() + 1);
                        //更新购物车数量
                        GeneralUtils.editCartNumber(goodsShopSPMobile.getId(),goodsShopSPMobileList.get(position).getNum());
                        //数量
                        holder.binding.etMagnitude.setText(goodsShopSPMobile.getNum() +"");
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(99999,goodsShopSPMobileList);
                        }
                    }
                }
            });
        }
    }


    @Override
    public int getItemCount() {

        return null == goodsShopSPMobileList ? 0 : goodsShopSPMobileList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemShopPingCarGoodsBinding binding;
        public ItemViewHolder(@NonNull ItemShopPingCarGoodsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
